package com.qyhl.webtv.commonlib.utils.router;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.R;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.PushParametersBean;
import com.qyhl.webtv.commonlib.service.UserService;


/**
 * 路由管理类
 *
 * @author helin
 * @date 2019年2月28日16:07:05
 */
public class RouterManager {

    /**
     * 跳转到登陆页面
     *
     * @param delay
     */
    public static void routerLogin(Context context, int delay) {
        new Handler().postDelayed(() ->
                ARouter.getInstance().build(ARouterPathConstant.LOGIN_ACTIVITY)
                        .greenChannel()
                        .withTransition(R.anim.open_next, R.anim.close_main)
                        .navigation(), delay);
    }

    public static void routerLoginInterceptor(Context context, String tag) {
        ARouter.getInstance()
                .build(tag)
                .navigation(context, new NavigationCallback() {
                    @Override
                    public void onFound(Postcard postcard) {

                    }

                    @Override
                    public void onLost(Postcard postcard) {

                    }

                    @Override
                    public void onArrival(Postcard postcard) {

                    }

                    @Override
                    public void onInterrupt(Postcard postcard) {
                        router(ARouterPathConstant.LOGIN_ACTIVITY);
                    }
                });
    }

    public static void routerLoginInterceptor(Context context, Bundle bundle, String tag) {
        CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
            @Override
            public void login(boolean isLogin) {
                if(isLogin){
                    router(tag,bundle);
                }else{
                    routerLogin(context,0);
                }

            }

            @Override
            public void error(String s) {

            }
        });
    }

    /**
     * 普通路由
     *
     * @param tag 跳转界面的标识
     */
    public static void router(String tag) {
        ARouter.getInstance().build(tag)
                .greenChannel()
                .withTransition(R.anim.open_next, R.anim.close_main)
                .navigation();
    }

    /**
     * 带参数跳转
     *
     * @param con
     * @param tag
     * @param in
     * @param out
     * @param code
     * @param bundle
     */
    public static void router(Activity con, String tag, int in, int out, int code, Bundle bundle) {
        ARouter.getInstance().build(tag)
                .with(bundle)
                .greenChannel()
                .withTransition(in, out)
                .navigation(con, code);
    }


    public static void router(String tag, Bundle bundle, int in, int out) {
        ARouter.getInstance().build(tag)
                .with(bundle)
                .greenChannel()
                .withTransition(in, out)
                .navigation();
    }

    /**
     * 携带参数跳转
     *
     * @param tag
     * @param bundle
     */
    public static void router(String tag, Bundle bundle) {
        ARouter.getInstance().build(tag)
                .with(bundle)
                .greenChannel()
                .withTransition(R.anim.open_next, R.anim.close_main)
                .navigation();
    }

    public static void router(Activity act, String tag, Bundle bundle, int code) {
        ARouter.getInstance().build(tag)
                .with(bundle)
                .greenChannel()
                .withTransition(R.anim.open_next, R.anim.close_main)
                .navigation(act, code);
    }

    public static void router(Activity act, String tag, int code) {
        ARouter.getInstance().build(tag)
                .greenChannel()
                .withTransition(R.anim.open_next, R.anim.close_main)
                .navigation(act, code);
    }

    /**
     * 跳转
     *
     * @param tag
     * @param fragment
     * @param code
     */
    public static void router(String tag, Fragment fragment, int code) {
        ARouter.getInstance().build(tag)
                .greenChannel()
                .withTransition(R.anim.open_next, R.anim.close_main)
                .navigation(fragment.getActivity(), code);
    }

    /**
     * 跳转
     *
     * @param tag
     * @param activity
     * @param code
     */
    public static void router(String tag, Activity activity, int code) {
        ARouter.getInstance().build(tag)
                .greenChannel()
                .withTransition(R.anim.open_next, R.anim.close_main)
                .navigation(activity, code);
    }

    /**
     * 渐隐渐现路由
     *
     * @param tag
     * @param context
     */
    public static void alphaRouter(String tag, Context context) {
        ARouter.getInstance().build(tag)
                .greenChannel()
                .withTransition(R.anim.alpha_open, R.anim.alpha_colse)
                .navigation();
    }


    /**
     * 推送跳转
     *
     * @param pushData
     * @param context
     */
    public static void skip(PushParametersBean pushData, Context context) {
        /**
         * type:1为普通新闻,2为图片新闻.3为直播新闻,4为标题新闻,5为视频新闻,7为广告,8为互动直播,9 爆料,101抓取的图文新闻,102抓取视频新闻
         */
        if (StringUtils.isNotEmpty(pushData.getType())) {
            switch (pushData.getType()) {
                case "1":
                    //普通新闻
                    ARouter.getInstance().build(ARouterPathConstant.NORMAL_NEWS_ACTIVITY)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .navigation(context);
                    break;
                case "2":
                    //图片新闻
                    ARouter.getInstance().build(ARouterPathConstant.PICTURE_NEWS_ACTIVITY)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .withString("section", pushData.getSectionID())
                            .navigation();
                    break;
                case "3":
                    //直播新闻
                    ARouter.getInstance().build(ARouterPathConstant.LIVE_NORMAL_ACTIVITY)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .withString("liveType", pushData.getLiveType())
                            .navigation();
                    break;
                case "5":
                    //视频新闻
                    ARouter.getInstance().build(ARouterPathConstant.VIDEO_NEWS_ACTIVITY)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .navigation();
                    break;
                case "6":
                    break;
                case "7":
                    //广告

                    break;
                case "8":
                    //互动直播
                    break;
                case "9":
                    //爆料
                    ARouter.getInstance().build(ARouterPathConstant.SCOOP_DETAIL_ACTIVIT)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .navigation();
                    break;
                case "10":
                    //图文直播
                    ARouter.getInstance().build(ARouterPathConstant.TELE_VIDEO_ACTIVITY)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .navigation();
                    break;
                case "101":
                    //抓取的图文新闻
                    ARouter.getInstance().build(ARouterPathConstant.NEWS_CATCH)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .navigation();
                    break;
                case "102":
                    //抓取视频新闻
                    ARouter.getInstance().build(ARouterPathConstant.SMALL_VIDEO_DETAIL_ACTIVITY)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .withString("id", pushData.getNewID())
                            .navigation();
                    break;
                default:
                    ARouter.getInstance().build(ARouterPathConstant.ACTIVITY_DEFAULT)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                    break;
            }
        }
    }

}
